package com.foolishbird.sdk.api;

import android.content.Context;

import com.foolishbird.sdk.utils.ToolsUtils;
import com.foolishbird.sdk.utils.WL;
import com.foolishbird.sdk.utils.XmlUtils;


public final class WanSDKConfiguration {

    private static final String TAG = WanSDKConfiguration.class.getSimpleName();

    private final int channelId;
    private final String gameId;
    private final Context context;
    private final int threadPoolSize;
    final int activityCount;
    final boolean isForbidAutoControl;
    final boolean isShowQuickRegister;

    private WanSDKConfiguration(final Builder builder){
        channelId = builder.channelId;
        gameId = builder.gameId;
        threadPoolSize = builder.threadPoolSize;
        context = builder.context;
        activityCount = builder.activityCount;
        isForbidAutoControl = builder.isForbidAutoControl;
        isShowQuickRegister = builder.isShowQuickRegister;
    }

    public static WanSDKConfiguration defaultCreate(Context context){
        return new Builder(context).build();
    }

    public int getChannelId(){
        return channelId;
    }

    public int getThreadPoolSize(){
        return threadPoolSize;
    }

    public String getGameId(){
        return gameId;
    }

    public Context getApplicationContext(){
        return context;
    }

    public static class Builder {
        private int channelId = -1;
        private String gameId = "0";
        private int threadPoolSize = 1;
        private int activityCount = 1;
        private Context context;
        private boolean isForbidAutoControl;
        private boolean isShowQuickRegister = true;
        private boolean isPutInChannel = false;

        public Builder(Context context){
            this.context = context.getApplicationContext();
        }

        public Builder setChannelId(int channelId){
            this.channelId = channelId;
            return this;
        }

        public Builder setGameId(String gameId){
            this.gameId = gameId;
            return this;
        }

        public Builder setPutInChannel(boolean isPutInChannel){
            this.isPutInChannel = isPutInChannel;
            return this;
        }

        public Builder setThreadPoolSize(int threadPoolSize){
            this.threadPoolSize = threadPoolSize;
            return this;
        }

        public Builder setAutoControlWindowEnabled(boolean enabled){
            this.isForbidAutoControl = !enabled;
            return this;
        }

        public Builder setActivityCount(int activityCount){
            this.activityCount = activityCount;
            return this;
        }

        public Builder setShowQuickRegisterEnabled(boolean enabled){
            this.isShowQuickRegister = enabled;
            return this;
        }

        public WanSDKConfiguration build(){
            initEmptyFieldsWithDefaultValues();
            return new WanSDKConfiguration(this);
        }

        private void initEmptyFieldsWithDefaultValues(){
            //TODO empty implementation, followed by extended use.
            if(channelId <= 0){
                if(!XmlUtils.isInitFinished()){
                    XmlUtils.parseConfigValue(context);
                }

                String channelStr = XmlUtils.getSdkChannelId();
                if(ToolsUtils.isNumberType(channelStr)){
                    channelId = Integer.parseInt(channelStr);
                }
                WL.d(TAG, "channelId is " + channelId);
            }
        }

    }


}
